package com.chaos.auth.service.oauth;

import com.chaos.auth.api.data.oauth.OauthClientTokenData;
import com.chaos.auth.entity.OauthClientToken;
import com.chaos.auth.mapper.OauthClientTokenMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chaos.framework.model.dto.page.PageList;
import com.chaos.framework.model.dto.page.PageQueryDto;
import com.chaos.framework.model.entity.Table;
import com.chaos.framework.model.helper.ConvertHelper;
import com.chaos.framework.model.helper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Jacky
 * @since 2024-05-07
 */
@Slf4j
@Component
public class OauthClientTokenService extends ServiceImpl<OauthClientTokenMapper, OauthClientToken> {

    public String insertModel(OauthClientTokenData data) {
        OauthClientToken entity = ConvertHelper.convert(data, OauthClientToken.class);
        return save(entity) ? entity.getAuthenticationId() : "";
    }

    public boolean deleteModel(String authenticationId) {
        return removeById(authenticationId);
    }

    public boolean updateModelByID(OauthClientTokenData data) {
        OauthClientToken entity = ConvertHelper.convert(data, OauthClientToken.class);
        return update(entity, new UpdateWrapper<OauthClientToken>().lambda()
                .eq(OauthClientToken::getAuthenticationId, data.getAuthenticationId()));
    }

    public OauthClientTokenData selectByID(String authenticationId) {
        return ConvertHelper.convert(getOne(new QueryWrapper<OauthClientToken>().lambda().eq(OauthClientToken::getAuthenticationId, authenticationId)), OauthClientTokenData.class);
    }

    public List<OauthClientTokenData> selectByData(OauthClientTokenData data) {
        QueryWrapper<OauthClientToken> query = new QueryWrapper();
        query.orderByDesc(Table.ID);
        return ConvertHelper.convert(list(query), OauthClientTokenData.class);
    }

    public PageList<OauthClientTokenData> selectByPage(PageQueryDto<OauthClientTokenData> pageData) {
        QueryWrapper<OauthClientToken> query = new QueryWrapper();
        query.orderByDesc(Table.ID);
        return PageHelper.pageList(page(PageHelper.page(pageData), query), OauthClientTokenData.class);
    }

}
